package Consumer;

import dbTools.DBTools;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * Servlet implementation class ConsumerDeleteserver
 */
@WebServlet("/ConsumerDeleteserver")
public class ConsumerDeleteserver extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ConsumerDeleteserver() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("textml;charset=GBK");
		request.setCharacterEncoding("GBK");
		response.setCharacterEncoding("GBK");
		String usersId =request.getParameter("usersId"); 
		int i=1;
		int j=0;
		DBTools db=new DBTools();
		db.open();
		String sqlI="Delete FROM userInfos WHERE usersId="+usersId;
		db.update(sqlI);
		String sql="SELECT * FROM userInfos";
		ResultSet rs=db.select(sql);
		ArrayList al=new ArrayList();
		try {
			while(rs.next()){
				ConsumerClassBean mcb=new ConsumerClassBean(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4));
				al.add(mcb);
			}
			if(al.size()%5!=0){
				j=al.size()/5+1;
				if(i<1){
					i=al.size()/5+1;
				}
				if(i>al.size()/5+1){
					i=1;
				}
			}else{
				j=al.size()/5;
				
				if(i<1){
					i=al.size()/5;
				}
				if(i>al.size()/5){
					i=1;
				}
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		String sql2="select * from userInfos limit "+(i-1)*5+","+"5";
		rs=db.select(sql2);
		ArrayList al1=new ArrayList();
		try {
			while(rs.next()){
				ConsumerClassBean mcb=new ConsumerClassBean(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4));
				al1.add(mcb);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.close();
		
		request.setAttribute("al", al1);
		request.setAttribute("i", i);
		request.setAttribute("j", j);
		request.getRequestDispatcher("/Generaluser.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("textml;charset=GBK");
		request.setCharacterEncoding("GBK");
		response.setCharacterEncoding("GBK");
		String usersId =request.getParameter("usersId"); 
		int i=1;
		int j=0;
		DBTools db=new DBTools();
		db.open();
		String sqlI="Delete FROM userInfos WHERE usersId="+usersId;
		db.update(sqlI);
		String sql="SELECT * FROM userInfos";
		ResultSet rs=db.select(sql);
		ArrayList al=new ArrayList();
		try {
			while(rs.next()){
				ConsumerClassBean mcb=new ConsumerClassBean(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4));
				al.add(mcb);
			}
			if(al.size()%5!=0){
				j=al.size()/5+1;
				if(i<1){
					i=al.size()/5+1;
				}
				if(i>al.size()/5+1){
					i=1;
				}
			}else{
				j=al.size()/5;
				
				if(i<1){
					i=al.size()/5;
				}
				if(i>al.size()/5){
					i=1;
				}
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		String sql2="select * from userInfos limit "+(i-1)*5+","+"5";
		rs=db.select(sql2);
		ArrayList al1=new ArrayList();
		try {
			while(rs.next()){
				ConsumerClassBean mcb=new ConsumerClassBean(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4));
				al1.add(mcb);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.close();
		
		request.setAttribute("al", al1);
		request.setAttribute("i", i);
		request.setAttribute("j", j);
		request.getRequestDispatcher("/Generaluser.jsp").forward(request, response);
	}

}
